package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.FragmentChangeManager;
import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerShoppingMallComponent;
import com.lzy.huijinjiazu.di.module.ShoppingMallModule;
import com.lzy.huijinjiazu.mvp.contract.ShoppingMallContract;
import com.lzy.huijinjiazu.mvp.presenter.ShoppingMallPresenter;
import com.lzy.huijinjiazu.mvp.ui.activity.ShoppingCartActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

//商城
public class ShoppingMallFragment extends BaseFragment<ShoppingMallPresenter> implements ShoppingMallContract.View {


    @BindView(R.id.tl_mall)
    SegmentTabLayout mTlMall;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    private FragmentChangeManager mFragmentChangeManager;

    public static ShoppingMallFragment newInstance() {
        ShoppingMallFragment fragment = new ShoppingMallFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingMallComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .shoppingMallModule(new ShoppingMallModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shopping_mall, container, false);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBar(getActivity(), mFlTitle);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        initFragment();

        mTlMall.setTabData(ArmsUtils.getStringArray(getContext(), R.array.mall_tab));
        mTlMall.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mFragmentChangeManager.setFragments(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void initFragment() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(ExtravagantMallFragment.newInstance());
        fragments.add(PersonalTailorFragment.newInstance());
        mFragmentChangeManager = new FragmentChangeManager(this.getChildFragmentManager(), R.id.fl_content, fragments);
        mFragmentChangeManager.setFragments(0);
    }


    @OnClick(R.id.iv_shopping_cart)
    public void onViewClicked() {
        ArmsUtils.startActivity(ShoppingCartActivity.class);
    }
}
